package com.gs.m_home.model

import android.content.Context
import android.util.Log
import com.gs.common.API
import com.gs.common.callback.HttpCallback
import com.gs.common.constant.HttpCode
import com.gs.common.constant.StateCode
import com.gs.common.global.GlobalVariables
import com.gs.common.sqlite.UserSqliteOpenHelper
import okhttp3.Call
import okhttp3.Response
import org.json.JSONObject
import java.net.ConnectException
import java.net.SocketTimeoutException

class MineModel(context: Context) {

    private val api by lazy { API.getInstance() }
    private val userSqliteOpenHelper by lazy { UserSqliteOpenHelper(context) }

    fun logout(block: (Int) -> Unit) {
        block.invoke(StateCode.STATE_LOADING)
        api.logout(GlobalVariables.loginUser?.userName ?: "", object : HttpCallback{
            override fun onSuccess(call: Call, result: String, response: Response) {
                val resultJSON = JSONObject(result)
                Log.d("code", "${resultJSON["code"]}")
                if (resultJSON["code"] == HttpCode.SUCCESS_CODE) {
                    userSqliteOpenHelper.deleteAll()
                    block.invoke(StateCode.STATE_SUCCESS)
                    return
                }
                block.invoke(StateCode.STATE_FAILED)
            }

            override fun onError(call: Call, e: Exception) {
                when(e) {
                    is ConnectException -> {
                        block.invoke(StateCode.NETWORK_ERROR)
                    }
                    is SocketTimeoutException -> {
                        block.invoke(StateCode.SERVER_ERROR)
                    }
                    else -> {
                        block.invoke(StateCode.UNKNOWN_ERROR)
                    }
                }
            }

        })
    }

    fun getUserInfo(block: (Int) -> Unit) {
        block.invoke(StateCode.STATE_LOADING)
        val username = GlobalVariables.loginUser?.userName ?: ""
        api.getUserInfoByUsername(username, object : HttpCallback{
            override fun onSuccess(call: Call, result: String, response: Response) {
                val resultJSON = JSONObject(result)
                Log.d("code", "${resultJSON["code"]}")
                Log.d("result", result)
                if (resultJSON["code"] == HttpCode.SUCCESS_CODE) {
                    GlobalVariables.loginUser?.nickname = if (resultJSON["nickname"].toString() == "null") {
                        "用户123"
                    } else {
                        resultJSON["nickname"].toString()
                    }
                    GlobalVariables.loginUser?.phoneNumber = resultJSON["phoneNumber"].toString();
                    GlobalVariables.loginUser?.address = resultJSON["address"].toString();
                    GlobalVariables.loginUser?.isBusiness = resultJSON["isBusiness"] as Boolean
                    block.invoke(StateCode.STATE_SUCCESS)
                    return
                }
                block.invoke(StateCode.STATE_FAILED)
            }

            override fun onError(call: Call, e: Exception) {
                when(e) {
                    is ConnectException -> {
                        block.invoke(StateCode.NETWORK_ERROR)
                    }
                    is SocketTimeoutException -> {
                        block.invoke(StateCode.SERVER_ERROR)
                    }
                    else -> {
                        block.invoke(StateCode.UNKNOWN_ERROR)
                    }
                }
            }

        })
    }

    companion object{
        private var instance: MineModel? = null
        fun getInstance(context: Context): MineModel {
            if (instance == null) {
                synchronized(this::class) {
                    if (instance == null) {
                        instance = MineModel(context)
                    }
                }
            }
            return instance!!
        }
    }
}